package com.tanhua.manager.listener;

import com.tanhua.autoconfig.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.mongo.PublishApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "tanhua-publish",consumerGroup = "tanhua-publish-group")
public class PublishCheckListener implements RocketMQListener<String> {


    @Reference
   private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;
    @Override
    public void onMessage(String publishId) {
//        通过publishId获取publish对象
        Publish publish = publishApi.findById(publishId);
//        publish不为空并且还未审核
        if(publish!=null&&publish.getState()==0){

 //            1、审核内容
            String textContent = publish.getTextContent();
            boolean contentIsPass = huaWeiUGCTemplate.textContentCheck(textContent);
//            2、审核图片
            String[] urls = publish.getMedias().toArray(new String[]{});
            boolean imageIsPass = huaWeiUGCTemplate.imageContentCheck(urls);
            int state = 1;
            if(contentIsPass&&imageIsPass){
//                内容和图片都审核通过了 修改publish的状态改成1
            }else{
//                内容或图片都没有审核通过 修改publish的状态改成2
                state = 2;
            }

            publishApi.updatePublishState(publishId,state);

            System.out.println("内容已审核完成.............");


        }


    }
}
